/*This file contains the ContactListFragment, 
 * with the list of the connected contacts. 
 * When you select an item from this list, the
 * ChatFragment is created/modified.
 * This file has no layout. */
package dk.aau.walkietalkieui.fragment;

import dk.aau.walkietalkieui.R;
import dk.aau.walkietalkieui.adapter.MenuListAdapter;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ContactListFragment extends ListFragment {

	public static final String TAG = "ContactListFragment";
	private MenuListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		adapter = new MenuListAdapter(getActivity());
		setListAdapter(adapter);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		if(adapter != null) {
			adapter.setSelectedItem(position);
		}
		
		try{
			((IContactListFragment)getActivity()).onMenuItemSelected(position);
		} catch (ClassCastException ce) {
			Log.e(TAG, getActivity().getString(R.string.parent_interface_error));
			
		} catch(Exception e) {
			Log.e(TAG, getActivity().getString(R.string.unhandled_error));
		}
	}
	
	public interface IContactListFragment{		
		public void onMenuItemSelected (int position);
	} 

}
